//
//  VisitorDetailControllerViewController.m
//  ppkc2016
//  商贸配对 - 展商--观众详情
//  Created by Laura on 2017/3/22.
//  Copyright © 2017年 拼凑的断音. All rights reserved.
//

#import "VisitorDetailControllerViewController.h"
#import "XXTool.h"
#import "PersonInfoHeadCell.h"
#import "VisitorDetailCell.h"
#import "VisitorDetailModel.h"
#import "LiuyanViewController.h"

@interface VisitorDetailControllerViewController () <UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tabview;
@property(nonatomic,strong) UIButton *liuyan;

@end

@implementation VisitorDetailControllerViewController{
    
    NSMutableArray<VisitorDetailModel *> *data;
    UIImage* userSelectedHead;
    NSString* headPhotoUrl;
    NSString* orderid;
    
}

-(UITableView*) tabview{
    if(_tabview == nil){
        _tabview =[[UITableView alloc]init];
        _tabview.delegate=self;
        _tabview.dataSource=self;
        _tabview.separatorColor = [XXTool colorWithLine];
        _tabview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [_tabview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        [self.view addSubview:_tabview];
    }
    return _tabview;
}

-(UIButton*) liuyan{
    if(_liuyan == nil){
        _liuyan=[[UIButton alloc]init];
        _liuyan.backgroundColor=[XXTool colorWithProject];
        [_liuyan setTitle:NSLocalizedString(@"留言", nil) forState:UIControlStateNormal];
        _liuyan.titleLabel.font=[UIFont systemFontOfSize:15.0];
        [_liuyan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_liuyan addTarget:self action:@selector(clickSendLiuyan) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:_liuyan];
    }
    return _liuyan;
}

-(void)clickSendLiuyan{
    [LiuyanViewController pushVistorLiuyanDetailWithSourceVC:self apmentID:orderid Type:XVCExhi];
}

-(void) initUI{
    [self.navigationItem setTitle:NSLocalizedString(@"观众名", nil)];
    [self tabview];
    [self liuyan];
    [self getData];
}

-(void)refreshLayout:(CGRect)frame{
    _tabview.frame=CGRectMake(0, 0, frame.size.width, frame.size.height-40);
    _liuyan.frame=CGRectMake(0, frame.size.height-40, frame.size.width, 40);
}

// 获取信息
-(void)getData{
    
    [VisitorDetailModel requestVisitorDetailInfoType:_visitorId completion:^(BOOL isSuccess, NSMutableArray<VisitorDetailModel *> *datalist ,NSString* orderId,NSString* head) {
        if(isSuccess){
            headPhotoUrl=head;
            orderid=orderId;
            data=datalist;
            [_tabview reloadData];
        }
        [self.view stopLoading];
    }];
    
}

#pragma mark tableviewDatasource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0){  //显示头像
        NSString *reuseIdentifer = @"PersonInfoHeadCell";
        PersonInfoHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
        if (cell==nil)
        {
            cell = [[PersonInfoHeadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifer];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.delegate = self;
        }
        if (userSelectedHead==nil)
        {
            [cell setUserHeadImg:headPhotoUrl compltion:^(UIImage *img) {
                userSelectedHead = img;
            }];
        }
        else
        {
            [cell setUserHeadImgWithUIImage:userSelectedHead];
        }
        return cell;
    }else{  // 显示 个人信息
        NSString *reuseIdentifer = @"PersonInfoInputCell";
        VisitorDetailCell *cell=[tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
        if(cell == nil){
            cell=[[VisitorDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifer];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        [cell setCellWitheData:data[indexPath.row-1]];
        return cell;
    }
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
        return 173;
    return 55;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1+data.count;
}

#pragma mark tableviewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    //    XMLProductInformationShowModel *temp = useData.list[indexPath.row];
    //    NSString *exName = @"";
    //    if ([XXTool didCurrentLanguageChinese])
    //    {
    //        exName = temp.cpname;
    //    }
    //    else
    //    {
    //        exName = temp.cpname_en;
    //    }
    //    [XVCProductDetailShow pushProductDetailVCWithSourceVC:self goods_id:temp.goods_id exhibitorName:exName];
}

+(void) pushVisitorDetailVc:(UIViewController*) sourceVc visitorId:(NSString*) vid{
    VisitorDetailControllerViewController *vd=[[VisitorDetailControllerViewController alloc]init];
    vd.visitorId=vid;
    
    if(sourceVc){
        [sourceVc.navigationController pushViewController:vd animated:YES];
    }
}

@end
